package com.yjd.bi.authority;

import com.yjd.bi.authority.model.AuthorityManagementModel;
import com.yjd.bi.authority.service.IAuthorityManagementService;
import com.yjd.bi.authority.vo.AuthorityUtil;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.system.model.AdminModel;
import com.yjd.comm.system.service.IAdminService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangchangyan@yijiedai.com on 2018/1/12.
 */
@Controller
@Scope("prototype")
@RequestMapping("/authority")
public class AuthorityManagementControl extends BaseControl {

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IAuthorityManagementService authorityManagementService;

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private IAdminService adminService;

    /**
     * 查询当前bi系统中可登陆的账号
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/list")
    public String userList() throws Exception {
        if( !super.getLoginUser().getUsername().equalsIgnoreCase("admin")){
            FrameUtil.throwBaseException("/report/weekly/list.action", "没有权限操作");
        }
        request.setAttribute("list", this.adminService.getAdminInfoDtos(FrameUtil.newHashMap("sort", "admin_id desc")));
        return "curent.system.user.list";
    }

    /**
     * 查询当前bi系统中可登陆的账号
     * @return
     * @throws Exception
     */
    @RequestMapping("/user/add")
    public String userAdd(Long id) throws Exception {
        Assert.notNull(id, "参数异常");
        Assert.notNull(adminService.getModelById(id, DbWREnums.READ), "用户不存在");
        Assert.isNull(authorityManagementService.getModelById(id, DbWREnums.READ), "不可重复添加");

        Long currentTime = FrameUtil.getTime();
        AuthorityManagementModel athity = new AuthorityManagementModel();
        athity.setAdmin_id(id);
        athity.setAuthority(0);
        athity.setCreate_time(currentTime);
        athity.setUpdate_time(currentTime);
        authorityManagementService.insert(athity);

        return "redirect:/authority/list.action";
    }

    @RequestMapping("/list")
    public String authorityList() throws Exception {
        if( !super.getLoginUser().getUsername().equalsIgnoreCase("admin")){
            FrameUtil.throwBaseException("/report/weekly/list.action", "没有权限操作");
        }
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("sort", "create_time desc");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.authorityManagementService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/authority/list"));

        List<Long> admin_ids = new ArrayList<Long>();
        List<AuthorityManagementModel> authorityList = pager.getPagerModel().getDataList();
        for(AuthorityManagementModel amm : authorityList){
            admin_ids.add(amm.getAdmin_id());
            amm.getStrMap().put("authorityList", AuthorityUtil.generateAuthorityList(amm.getAuthority()));
            amm.getStrMap().put("create_time", DateUtil.timestampToDateString(amm.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
            amm.getStrMap().put("update_time", DateUtil.timestampToDateString(amm.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        Map<Long, AdminModel> admins = this.adminService.getModelMapByIds(admin_ids, DbWREnums.READ);
        for(AuthorityManagementModel amm : authorityList){
            amm.getStrMap().put("username", admins.get(amm.getAdmin_id()).getUsername());
        }

        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);
        return "authority.mgmt.list";
    }

    /**
     * 修改权限
     * @param id
     * @param athity
     * @throws Exception
     */
    @RequestMapping("/save")
    public void authoritySave(Integer id, int athity) throws Exception {
        if( !super.getLoginUser().getUsername().equalsIgnoreCase("admin")){
            FrameUtil.throwBaseException1("只有管理员才可以配置权限");
        }

        if(null != id){
            AdminModel admin = adminService.getModelById(id, DbWREnums.READ);
            Assert.notNull(admin, "用户不存在");

            AuthorityManagementModel ath = authorityManagementService.getModelById(id, DbWREnums.READ);
            Assert.notNull(ath, "用户不存在");
            ath.setAuthority(athity);
            ath.setUpdate_time(FrameUtil.getTime());
            authorityManagementService.update(ath);
        }

        this.successMsg("修改权限成功");
    }

    @RequestMapping("/delete")
    public String authorityDelete(Integer id) throws Exception {
        if(null != id){
            if( !super.getLoginUser().getUsername().equalsIgnoreCase("admin")){
                FrameUtil.throwBaseException1("只有管理员可操作");
            }
            authorityManagementService.delete(id);
        }
        return "redirect:/authority/list.action";
    }
}
